package com.lemon.system.service.impl;

import com.lemon.mybatisplus.base.BaseServiceImpl;
import com.lemon.mybatisplus.enums.DeleteEnum;
import com.lemon.mybatisplus.enums.StatusEnum;
import com.lemon.system.dao.MenuResourceDAO;
import com.lemon.system.dto.MenuResourceDTO;
import com.lemon.system.po.MenuResourcePO;
import com.lemon.system.service.IMenuResourceService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 菜单-接口中间表 业务实现类
 *
 * @author 李猛
 * @since 2024-02-11
 */
@Log4j2
@Service
public class MenuResourceServiceImpl extends BaseServiceImpl<MenuResourceDAO, MenuResourcePO, MenuResourceDTO> implements IMenuResourceService {
    @Override
    public List<String> getResourceByUserId(Long userId, Long tenantId) {
        Map<String, Object> param = new HashMap<>();
        param.put("userId", userId);
        param.put("tenantId", tenantId);
        param.put(StatusEnum.ENABLE.getColumn(), StatusEnum.ENABLE.getValue());
        param.put(DeleteEnum.UNABLE.getColumn(), DeleteEnum.UNABLE.getValue());
        List<String> result = this.baseMapper.getResourceByUserId(param);
        if (CollectionUtils.isEmpty(result)) {
            return null;
        }
        return result.stream().map(item -> item.startsWith("/") ? item : ("/" + item)).distinct().collect(Collectors.toList());
    }
}